package com.gxa.zhonglong.controller;

import com.gxa.zhonglong.common.JsonResult;
import com.gxa.zhonglong.exception.BusinessException;
import com.gxa.zhonglong.pojo.ProductCode;
import com.gxa.zhonglong.service.ProductCodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
@Api("产品编码接口")
@CrossOrigin
@RestController
@RequestMapping("/product")
public class ProductCodeController {
    @Autowired
    private ProductCodeService productCodeService=null;
    @ApiOperation(value = "产品编码表添加")
    @PostMapping("/insert")
    @ApiImplicitParams(
                @ApiImplicitParam(name ="productCode",value = "产品编码表的添加对象",required = true)
    )
    public Map insert( ProductCode productCode) throws BusinessException {
        System.out.println(productCode);
      productCodeService.insert(productCode);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }
    @ApiOperation(value = "产品编码表全查询")
    @PostMapping("/getList")
    public Map selectAll(){
        List<Map<String,Object>> select = productCodeService.select();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(select);
        return jsonResult.getDataMap();
    }
    @ApiOperation(value = "产品编码表单个详情查询")
    @PostMapping("/get")
    @ApiImplicitParams(
            @ApiImplicitParam(name ="productId",value = "产品编码表的Id",required = true,paramType = "long")
    )
    public Map selectById(@RequestParam Long productId){
        ProductCode productCode = productCodeService.selectById(productId);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(productCode);
        return jsonResult.getDataMap();
    }

    @ApiOperation(value = "产品编码表关键字查询")
    @PostMapping("/getNameList")
    @ApiImplicitParams(
            @ApiImplicitParam(name ="productName",value = "商品名称",required = true,paramType = "string")
    )
    public Map selectByName(@RequestParam("productName") String productName){
        List<Map<String,Object>> productCodes = productCodeService.selectByName(productName);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(productCodes);
        return jsonResult.getDataMap();
    }
    @ApiOperation(value = "产品编码表的单个删除")
    @PostMapping("/delete")
    @ApiImplicitParams(
            @ApiImplicitParam(name ="productId",value = "产品编码表的Id",required = true,paramType = "long")
    )
    public Map delete(@RequestParam("productId") Long productId) throws BusinessException {
    productCodeService.delete(productId);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }
    @ApiOperation(value = "产品编码表的选择批量删除")
    @PostMapping("/deleteChoose")
    @ApiImplicitParams(
            @ApiImplicitParam(name ="map",value = "产品编码表的Id",required = true,paramType = "body")
    )
    public Map deleteChoose(@RequestBody Map<String,Object> map){
        List list = (List)map.get("list");
        productCodeService.deleteChoose(list);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }
}
